﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Lawyer.DAL
{
	/// <summary>
	/// 数据访问类:AgencyInfo
	/// </summary>
	public partial class AgencyInfo
	{
		public AgencyInfo()
		{}
		#region  Method

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string AgencyID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from AgencyInfo");
			strSql.Append(" where AgencyID=@AgencyID ");
			SqlParameter[] parameters = {
					new SqlParameter("@AgencyID", SqlDbType.VarChar,36)};
			parameters[0].Value = AgencyID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Lawyer.Model.AgencyInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into AgencyInfo(");
			strSql.Append("AgencyID,Name,AgencyRate,Email,City,Telphone,Fax,Address,AvatarURL,Status,Note,CreatedBy,CreatedDate,Updatedby,UpdatedDate)");
			strSql.Append(" values (");
			strSql.Append("@AgencyID,@Name,@AgencyRate,@Email,@City,@Telphone,@Fax,@Address,@AvatarURL,@Status,@Note,@CreatedBy,@CreatedDate,@Updatedby,@UpdatedDate)");
			SqlParameter[] parameters = {
					new SqlParameter("@AgencyID", SqlDbType.VarChar,36),
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@AgencyRate", SqlDbType.Float,8),
					new SqlParameter("@Email", SqlDbType.VarChar,50),
					new SqlParameter("@City", SqlDbType.VarChar,50),
					new SqlParameter("@Telphone", SqlDbType.VarChar,20),
					new SqlParameter("@Fax", SqlDbType.VarChar,20),
					new SqlParameter("@Address", SqlDbType.VarChar,100),
					new SqlParameter("@AvatarURL", SqlDbType.VarChar,100),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@Note", SqlDbType.VarChar,200),
					new SqlParameter("@CreatedBy", SqlDbType.VarChar,36),
					new SqlParameter("@CreatedDate", SqlDbType.DateTime),
					new SqlParameter("@Updatedby", SqlDbType.VarChar,36),
					new SqlParameter("@UpdatedDate", SqlDbType.DateTime)};
			parameters[0].Value = model.AgencyID;
			parameters[1].Value = model.Name;
			parameters[2].Value = model.AgencyRate;
			parameters[3].Value = model.Email;
			parameters[4].Value = model.City;
			parameters[5].Value = model.Telphone;
			parameters[6].Value = model.Fax;
			parameters[7].Value = model.Address;
			parameters[8].Value = model.AvatarURL;
			parameters[9].Value = model.Status;
			parameters[10].Value = model.Note;
			parameters[11].Value = model.CreatedBy;
			parameters[12].Value = model.CreatedDate;
			parameters[13].Value = model.Updatedby;
			parameters[14].Value = model.UpdatedDate;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Lawyer.Model.AgencyInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update AgencyInfo set ");
			strSql.Append("Name=@Name,");
			strSql.Append("AgencyRate=@AgencyRate,");
			strSql.Append("Email=@Email,");
			strSql.Append("City=@City,");
			strSql.Append("Telphone=@Telphone,");
			strSql.Append("Fax=@Fax,");
			strSql.Append("Address=@Address,");
			strSql.Append("AvatarURL=@AvatarURL,");
			strSql.Append("Status=@Status,");
			strSql.Append("Note=@Note,");
			strSql.Append("CreatedBy=@CreatedBy,");
			strSql.Append("CreatedDate=@CreatedDate,");
			strSql.Append("Updatedby=@Updatedby,");
			strSql.Append("UpdatedDate=@UpdatedDate");
			strSql.Append(" where AgencyID=@AgencyID ");
			SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@AgencyRate", SqlDbType.Float,8),
					new SqlParameter("@Email", SqlDbType.VarChar,50),
					new SqlParameter("@City", SqlDbType.VarChar,50),
					new SqlParameter("@Telphone", SqlDbType.VarChar,20),
					new SqlParameter("@Fax", SqlDbType.VarChar,20),
					new SqlParameter("@Address", SqlDbType.VarChar,100),
					new SqlParameter("@AvatarURL", SqlDbType.VarChar,100),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@Note", SqlDbType.VarChar,200),
					new SqlParameter("@CreatedBy", SqlDbType.VarChar,36),
					new SqlParameter("@CreatedDate", SqlDbType.DateTime),
					new SqlParameter("@Updatedby", SqlDbType.VarChar,36),
					new SqlParameter("@UpdatedDate", SqlDbType.DateTime),
					new SqlParameter("@AgencyID", SqlDbType.VarChar,36)};
			parameters[0].Value = model.Name;
			parameters[1].Value = model.AgencyRate;
			parameters[2].Value = model.Email;
			parameters[3].Value = model.City;
			parameters[4].Value = model.Telphone;
			parameters[5].Value = model.Fax;
			parameters[6].Value = model.Address;
			parameters[7].Value = model.AvatarURL;
			parameters[8].Value = model.Status;
			parameters[9].Value = model.Note;
			parameters[10].Value = model.CreatedBy;
			parameters[11].Value = model.CreatedDate;
			parameters[12].Value = model.Updatedby;
			parameters[13].Value = model.UpdatedDate;
			parameters[14].Value = model.AgencyID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string AgencyID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from AgencyInfo ");
			strSql.Append(" where AgencyID=@AgencyID ");
			SqlParameter[] parameters = {
					new SqlParameter("@AgencyID", SqlDbType.VarChar,36)};
			parameters[0].Value = AgencyID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string AgencyIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from AgencyInfo ");
			strSql.Append(" where AgencyID in ("+AgencyIDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Lawyer.Model.AgencyInfo GetModel(string AgencyID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 AgencyID,Name,AgencyRate,Email,City,Telphone,Fax,Address,AvatarURL,Status,Note,CreatedBy,CreatedDate,Updatedby,UpdatedDate from AgencyInfo ");
			strSql.Append(" where AgencyID=@AgencyID ");
			SqlParameter[] parameters = {
					new SqlParameter("@AgencyID", SqlDbType.VarChar,36)};
			parameters[0].Value = AgencyID;

			Lawyer.Model.AgencyInfo model=new Lawyer.Model.AgencyInfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["AgencyID"]!=null && ds.Tables[0].Rows[0]["AgencyID"].ToString()!="")
				{
					model.AgencyID=ds.Tables[0].Rows[0]["AgencyID"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Name"]!=null && ds.Tables[0].Rows[0]["Name"].ToString()!="")
				{
					model.Name=ds.Tables[0].Rows[0]["Name"].ToString();
				}
				if(ds.Tables[0].Rows[0]["AgencyRate"]!=null && ds.Tables[0].Rows[0]["AgencyRate"].ToString()!="")
				{
                    model.AgencyRate = Double.Parse(ds.Tables[0].Rows[0]["AgencyRate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Email"]!=null && ds.Tables[0].Rows[0]["Email"].ToString()!="")
				{
					model.Email=ds.Tables[0].Rows[0]["Email"].ToString();
				}
				if(ds.Tables[0].Rows[0]["City"]!=null && ds.Tables[0].Rows[0]["City"].ToString()!="")
				{
					model.City=ds.Tables[0].Rows[0]["City"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Telphone"]!=null && ds.Tables[0].Rows[0]["Telphone"].ToString()!="")
				{
					model.Telphone=ds.Tables[0].Rows[0]["Telphone"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Fax"]!=null && ds.Tables[0].Rows[0]["Fax"].ToString()!="")
				{
					model.Fax=ds.Tables[0].Rows[0]["Fax"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Address"]!=null && ds.Tables[0].Rows[0]["Address"].ToString()!="")
				{
					model.Address=ds.Tables[0].Rows[0]["Address"].ToString();
				}
				if(ds.Tables[0].Rows[0]["AvatarURL"]!=null && ds.Tables[0].Rows[0]["AvatarURL"].ToString()!="")
				{
					model.AvatarURL=ds.Tables[0].Rows[0]["AvatarURL"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Status"]!=null && ds.Tables[0].Rows[0]["Status"].ToString()!="")
				{
					model.Status=int.Parse(ds.Tables[0].Rows[0]["Status"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Note"]!=null && ds.Tables[0].Rows[0]["Note"].ToString()!="")
				{
					model.Note=ds.Tables[0].Rows[0]["Note"].ToString();
				}
				if(ds.Tables[0].Rows[0]["CreatedBy"]!=null && ds.Tables[0].Rows[0]["CreatedBy"].ToString()!="")
				{
					model.CreatedBy=ds.Tables[0].Rows[0]["CreatedBy"].ToString();
				}
				if(ds.Tables[0].Rows[0]["CreatedDate"]!=null && ds.Tables[0].Rows[0]["CreatedDate"].ToString()!="")
				{
					model.CreatedDate=DateTime.Parse(ds.Tables[0].Rows[0]["CreatedDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Updatedby"]!=null && ds.Tables[0].Rows[0]["Updatedby"].ToString()!="")
				{
					model.Updatedby=ds.Tables[0].Rows[0]["Updatedby"].ToString();
				}
				if(ds.Tables[0].Rows[0]["UpdatedDate"]!=null && ds.Tables[0].Rows[0]["UpdatedDate"].ToString()!="")
				{
					model.UpdatedDate=DateTime.Parse(ds.Tables[0].Rows[0]["UpdatedDate"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select AgencyID,Name,AgencyRate,Email,City,Telphone,Fax,Address,AvatarURL,Status,Note,CreatedBy,CreatedDate,Updatedby,UpdatedDate ");
			strSql.Append(" FROM AgencyInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" AgencyID,Name,AgencyRate,Email,City,Telphone,Fax,Address,AvatarURL,Status,Note,CreatedBy,CreatedDate,Updatedby,UpdatedDate ");
			strSql.Append(" FROM AgencyInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "AgencyInfo";
			parameters[1].Value = "AgencyID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

